import java.awt.Font;
import java.util.Scanner;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 * @author Jovan Munroe
 * @date 11/29/2013
 * Software Maintenance on Virtual Campfire Story
 * Increase usability by providing a GUI Frontend
 */
public class CampfireGUI {

	//Abstraction Component.
	public static AvailableStoriesAgent aSA = new AvailableStoriesAgent();

	/* In the main method of CampfireStory, the presentation component of this top-level PAC agent is initiated frist,
	 *	with a call to printMenu(). The aSA variable is the abstraction component and is used to check for available stories.
	 *  The main method itself is the control component: taking user input and activating the appropriate sub-agents.
	 */
	public static void main(String[] args) {
		int choice;
		Scanner in = new Scanner(System.in);

		//splash screen
		final JLabel label = new JLabel("", JLabel.CENTER);
		label.setText("<html><center>Welcome to Virtual Campfire Stories<br><br>Share stories with others on the same network!</center></html>");
		label.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 20));
		JOptionPane.showMessageDialog(null, label, "Virtual Campfire Stories", JOptionPane.PLAIN_MESSAGE);

		while(true)	//Main program loop
		{
			choice = printMenu(aSA);	//Print instructions and open stories.
			System.out.println(choice);

			if (choice == 0)	//If authoring a story is desired, create a new CreateStoryAgent, and run it's control component.
			{
				CreateStoryAgent cSA = new CreateStoryAgent();
				cSA.authorStory();
			}
			else if(choice == 1) //Refresh current running stories list, if requested.
			{
				label.setText("Checking for open stories....");
				JOptionPane.showMessageDialog(null, label, "Checking Stories", JOptionPane.PLAIN_MESSAGE);
				System.out.println("\nChecking for open stories...\n");
				int roomNum = aSA.displayRunningStories();
				if (roomNum != 0 ) {
					joinRoom(roomNum);
				}
			}
			else if(choice == 2)	//Exit, if requested.
			{
				label.setText("See you next time!");
				JOptionPane.showMessageDialog(null, label, "Bye, Bye!", JOptionPane.PLAIN_MESSAGE);
				System.exit(0);
			} else {
				System.out.println("Invalid input.");
			}
		}
	}

	//Presentation component of the CampfireStory top-level PAC agent.
	//Prints a menu, along with the running stories.
	private static int printMenu(AvailableStoriesAgent aSA)
	{
		final JLabel label = new JLabel("Please choose an option!", JLabel.CENTER);
		label.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 20));

		Object[] options = {"Create Story","Refresh List of Stories","Exit"};
		int n = JOptionPane.showOptionDialog(null,label,
				"Virtual Campfire Main Menu",
				JOptionPane.YES_NO_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE,
				null,
				options,
				options[2]);
		return n;
	}

	private static void joinRoom(int room)
	{
		JoinStoryAgent jSA = new JoinStoryAgent(aSA.getStoryConnection(room));
		Thread jSAT = new Thread(jSA);
		jSAT.start();
		try {
			jSAT.join();
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}